﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DispatchAssist.BusinessLogical.Model;

namespace DispatchAssist.Model
{
    public class CacheSeatLogin
    {
        private List<SeatLogin> _list;

        public CacheSeatLogin(List<SeatLogin> list)
        {
            _list = list;
        }

        public SeatLogin GetCurrentSeatInfo(string UserID)
        {
            var items = _list.Where(f => (f.UserID.ToString() == UserID) && (f.IsLogout == false));

            if (items == null || items.Count() == 0)
            {
                return null;
            }
            else
            {
                return items.ToList()[0];
            }
        }

        public List<SeatLogin> GetSeatLoginInfoList(string SeatID)
        {
            var items = _list.Where(f => (f.SeatID == new Guid(SeatID)) && (f.IsLogout == false));

            if (items == null || items.Count() == 0)
            {
                return null;
            }
            else
            {
                return items.ToList();
            }
        }

        public string GetCurrentSeatID(string UserID)
        {
            SeatLogin seat = GetCurrentSeatInfo(UserID);

            if (seat == null)
            {
                return string.Empty;
            }
            else
            {
                return seat.SeatID.ToString();
            }
        }


        public List<string> GetRelatedSeatIDs(string UserID)
        {
            var items = _list.Where(f=>f.UserID.ToString()==UserID);

            if (items == null)
            {
                return new List<string>();
            }

            return items.Distinct(new Comparints()).ToDictionary<SeatLogin, string>(f => f.SeatID.ToString()).Keys.ToList();

        }


    }
    public class Comparints : IEqualityComparer<SeatLogin>
    {

        public bool Equals(SeatLogin x, SeatLogin y)
        {
            if (x == null && y == null)
                return false;
            return x.SeatID == y.SeatID;
        }

        public int GetHashCode(SeatLogin obj)
        {
            return obj.ToString().GetHashCode();
        }
    }
}
